package managerGUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import managerService.ManagerSystem;
import model.Project;
import model.Type;

public class ManagerMenu extends JMenuBar {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	JMenu start;
	
	JMenuItem newProject;
	JMenuItem newType;
	JMenuItem setPath;
	JMenuItem exit;
	
	public ManagerMenu(){
		super();
		start = new JMenu("Start");
		newProject = start.add("Create new project");
		newType = start.add("add a new project type");
		setPath = start.add("reconfigure XML path");
		exit = start.add("exit");		
		
		start.setMnemonic('S');
		newProject.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, 
				KeyEvent.CTRL_DOWN_MASK));        
		newType.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,
				KeyEvent.CTRL_DOWN_MASK));
		setPath.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
				KeyEvent.CTRL_DOWN_MASK));
		exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
				KeyEvent.CTRL_DOWN_MASK));                  
		
		newProject.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (MainInterface.instance != null){
					Project p = new Project();
					//new SetPathPopup();
					new CreateProjectPopup(p);
//					if (p.getName() != null)
//						MainInterface.instance.setPanel(new ManagerPanel(p));
				}
			}
		});
		
		newType.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String name = JOptionPane.showInputDialog("name of the new type : ");
				if (name != null && !name.equals(""))
					ManagerSystem.getService().addType(new Type(name));
			}
		});
		
		setPath.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new SetPathPopup();
			}
			
		});
		
		exit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int option = JOptionPane.showConfirmDialog(null,
						"Exit program?",
						"Exit",
						JOptionPane.OK_CANCEL_OPTION,
						JOptionPane.QUESTION_MESSAGE);
				if (option == JOptionPane.OK_OPTION)
					System.exit(0);	
			}
		});
		
		this.add(start);
	}
	
	
}
